<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 面包屑 -->
  <div slot="header" class="clearfix" v-if="$route.query.name">
    <el-breadcrumb separator-class="el-icon-arrow-right" >
      <el-breadcrumb-item > {{$route.query.name}} </el-breadcrumb-item>
      <el-breadcrumb-item > java </el-breadcrumb-item>
      <el-breadcrumb-item > 目录管理 </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
      <!-- 头部搜索栏 -->
      <el-col :span="13">
        <div class="grid-content bg-purple">
          <el-form ref="form"  label-width="80px" inline>
            <el-form-item label="目录名称" >
              <el-input v-model="info.directoryName" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="info.state" placeholder="请选择">
                <el-option label="开启" :value="1"></el-option>
                <el-option label="屏蔽" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="resetForm">清除</el-button>
              <el-button type="primary" @click="getList">筛选</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="2" :push='9' style="display:flex;justify-content:flex-end;">
          <div class="grid-content bg-purple-light">
            <el-button type="text" v-if="$route.query.name">返回学科</el-button>
            <el-button type="success" @click="addDirectorys('新增目录')">新增目录</el-button>
          </div>
      </el-col>
      <!-- 总条数提示栏 -->
      <el-alert
        type="info"
        show-icon
        :closable='false'>
        数据一共{{counts}}条
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="directorysList"
        style="width: 100%">
        <el-table-column
          prop="index"
          type="index"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="subjectName"
          label="所属学科"
          width="180">
        </el-table-column>
        <el-table-column
          label="目录名称"
          prop="directoryName">
        </el-table-column>
        <el-table-column
          prop="username"
          label="创建者">
        </el-table-column>
        <el-table-column
          label="创建日期">
          <template #default='{row}'>
            {{row.addDate | parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totals"
          label="面试题数量">
        </el-table-column>
        <el-table-column
          label="状态"
          prop="stringState">
        </el-table-column>
        <el-table-column
          label="操作" width='200px'>
          <template #default='{row}'>
            <el-button type="text" @click="stateStyle(row.state,row.id)"> {{row.stringState}} </el-button>
            <el-button type="text" :disabled='row.state===0 ? true : false'  @click="addDirectorys('修改目录',{directoryName:row.directoryName,subjectID:row.subjectID,id:row.id})">修改</el-button>
            <el-button type="text" :disabled='row.state===0 ? true : false' @click="delQuestionsList(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="directorysRequest.page"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="directorysRequest.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
      >
      </el-pagination>
      <!--  弹出框  -->
      <el-dialog
        :title="addOrEdit"
        :visible.sync="dialogVisible"
        width="30%"
        @close='closeDialog'
        >
        <el-form ref="form"  label-width="80px">
          <el-form-item label="所属学科">
            <el-select  placeholder="请选择" v-model='SubjectInfo.subjectID'>
              <el-option v-for="item in simpleList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
            <el-form-item label="目录名称">
              <el-input style="width:226px" v-model="SubjectInfo.directoryName"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="addOrEditFn">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, update, changeState, remove } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { status as statusStyle } from '@/api/hmmm/constants.js'
export default {
  name: 'Questions-randoms',
  data () {
    return {
      SubjectInfo: {
        subjectID: null,
        directoryName: null
      },
      addOrEdit: '新增目录',
      directorysRequest: {
        page: 1,
        pagesize: 10
      },
      info: {
        subjectID: null,
        directoryName: null,
        state: null
      },
      counts: 0,
      directorysList: [],
      dialogVisible: false,
      flag: false,
      simpleList: []
    }
  },
  methods: {
    // 清除关键词搜索栏
    resetForm () {
      this.info = {
        subjectID: null,
        directoryName: null,
        state: null
      }
      this.$refs.form.resetFields()
    },
    // 获取目录列表
    async getList () {
      console.log({ ...this.directorysRequest, ...this.info })
      const { status, data } = await list({ ...this.directorysRequest, ...this.info })
      console.log(status, data)
      if (status === 200) {
        this.counts = data.counts
        data.items = data.items.map(item => {
          statusStyle.forEach(i => {
            if (i.value === item.state) {
              item.stringState = i.label
            }
          })
          return item
        })
        this.directorysList = data.items
      }
    },
    // 删除所选题目
    delQuestionsList (id) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const obj = {
          id
        }
        console.log(id)
        const res = await remove(obj)
        console.log(res)
        if (res.status === 200) {
          if (this.directorysList.length <= 1) {
            this.directorysRequest.page--
          }
          if (this.directorysList.length <= 1 && this.directorysRequest.page === 1) {
            this.directorysRequest.page = 1
          }
          this.getList()
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    // 页数改变触发
    handleCurrentChange (page) {
      // console.log(page)
      this.directorysRequest.page = page
      console.log(this.directorysRequest.page)
    },
    // 每页数量改变触发
    handleSizeChange (pagesize) {
      this.directorysRequest.page = 1
      this.directorysRequest.pagesize = pagesize
      console.log(this.directorysRequest.pagesize)
    },
    // 改变启用禁用状态
    async stateStyle (sta, id) {
      console.log(sta)
      const obj = { id }
      if (sta === 1) {
        obj.state = 0
      } else {
        obj.state = 1
      }
      const res = await changeState(obj)
      console.log(res)
      this.getList()
    },
    // 关闭对话框
    closeDialog () {
      this.dialogVisible = false
      this.flag = false
      this.SubjectInfo = {
        subjectID: null,
        directoryName: null
      }
    },
    // 打开新增对话框
    async addDirectorys (title, obj) {
      console.log('新增')
      const res = await simple()
      console.log(res)
      this.simpleList = res.data
      this.addOrEdit = title
      if (this.addOrEdit === '修改目录') {
        this.SubjectInfo.subjectID = obj.subjectID
        this.SubjectInfo.directoryName = obj.directoryName
        this.SubjectInfo.id = obj.id
        console.log(this.SubjectInfo)
      }
      this.dialogVisible = true
    },
    // 添加或者编辑新目录
    async addOrEditFn () {
      if (this.title === '新增目录') {
        const res = await add(this.SubjectInfo)
        console.log(res)
      } else {
        const res = await update(this.SubjectInfo)
        console.log(res)
      }
      this.getList()
      this.closeDialog()
    }
  },
  // 监听器监听页数变化
  watch: {
    directorysRequest: {
      deep: true,
      immediate: true,
      handler (newValue) {
        this.getList()
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.box-card {
  margin: 0 20px;
}
</style>
